/*
 *      Copyright (c) 2007 Marcelo S. Araujo
 *      All rights reserved.
 *
 *      Redistribution and use in source and binary forms, with or without
 *      modification, are permitted provided that the following conditions
 *      are met:
 *      1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *      2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 *      THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *      ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *      IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *      ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 *      FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 *      DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 *      OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *      HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *      LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *      OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 *      SUCH DAMAGE.
 *
 */

void GetPR(string PrNumber);
void ClosePR(string PrNumber);
void TakePR(string PrNumber);
void PRFile(char PrNumber[20]);

void ClosePR(string PrNumber) {
	string sendstring = "ssh " + FileConfs.USERNAME + "@" + FileConfs.SSH_HOST + " ~/bin/close-pr " + PrNumber;
	system(sendstring.c_str());
};

void TakePR(string PrNumber) {
	string sendstring = "ssh " + FileConfs.USERNAME + "@" + FileConfs.SSH_HOST + " ~/bin/take-pr " + PrNumber;
	system(sendstring.c_str());
}

void GetPR(string PrNumber) {
	string sendstring = "ssh " + FileConfs.USERNAME + "@" + FileConfs.SSH_HOST + " query-pr -F " + PrNumber + " >" + PrNumber;
	system(sendstring.c_str());
};

void PRFile(char PrNumber[20]) {
	string line;
	int line_begins, line_ends = 0;
	int begins, ends = 0;
	static int count = 0;
	ifstream ReadFile(PrNumber);
	ifstream RFTPrint(PrNumber);
	while(!ReadFile.eof()) {
		getline(ReadFile, line);
		begins = line.find("begins here");
		ends = line.find("ends here");
		if(begins != string::npos) {
			line_begins = count + 1;
		}
		if(ends != string::npos) {
			line_ends = count + 1;
		}
		count = count + 1;
	}
	for(int n = 0 ; n < count; n++) {
		getline(RFTPrint, line);
		if(n >= line_begins && n <= line_ends) {
			cout << line << endl;
		}
	}
	ReadFile.close();
	RFTPrint.close();
};

// Use: portmenu getpr 119412 > pr.patch
